/* demonstrate how to use thread and how to use fifo etc
  * 1. write thread is main thread
  * 2. create a read thread
  * 3. use fifo to commnicate
  * 4. display processID and threadID
  */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

#include "msg_type.h"

#define FIFO_NAME "/tmp/FIFO"
//#define BUFFER_SIZE 256 //4096
#define BUFFER_SIZE sizeof(MSG_DATA)
//#define TOTAL_LEN 40960
void *foo(void *)
{
       printf("foo thread begin\n");

  //fifo 的读写就是按普通的文件来操作的.
       int pipe_fd = open(FIFO_NAME, O_RDONLY); // when open pipe, main thread will continue run
     printf("Process %d opened read fd %d\n", getpid(), pipe_fd);

     char buffer[BUFFER_SIZE + 1];
     int res;
     int bytes_read = 0;
     if(pipe_fd != -1)
       {
            do
            {
                 res = read(pipe_fd, buffer, BUFFER_SIZE);
                 bytes_read += res;
            }while(res > 0);
       }
       else
       {
            perror("reason:");
       }
       close(pipe_fd);
     printf("Process %d, threadID %ld, bytes read %d\n", getpid(), pthread_self(),bytes_read);
       return NULL;
}
void createReadThread()
{
       pthread_t id;
       int ret=pthread_create(&id,NULL,foo,NULL); //创建一个thread foo
       if(ret != 0)
       {
            perror("reason:");
       }
}

#if 1
int main()
{
     if(access(FIFO_NAME, F_OK) == -1)
     {
           int res = mkfifo(FIFO_NAME, 0777); //创建一个特殊的文件,FIFO
           if(res != 0)
           {
                 fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
                 exit(EXIT_FAILURE);
           }
     }

     int bytes_sent = 0;
     char buffer[BUFFER_SIZE + 1];
     printf("Process %d opening FIFO O_WRONLY\n", getpid());
       createReadThread();

   //打开FIFO 准备写
     int pipe_fd = open(FIFO_NAME, O_WRONLY);   // will block here, wait someone open the FIFO for read!
     printf("Process %d opened write fd %d\n", getpid(), pipe_fd);
     if(pipe_fd != -1)
     {
            struct timespec time1 = {0, 0}; 
    struct timespec time2 = {0, 0};

    float temp;
        bytes_sent=0;
        clock_gettime(CLOCK_REALTIME, &time1);
           while(bytes_sent < def_TEST_CNT)
           { //FIFO 就是按普通文件描述符来操作的.
                 int res = write(pipe_fd, buffer, BUFFER_SIZE);
                 if(res == -1)
                 {
                     fprintf(stderr, "Write error on pipe\n");
                     exit(EXIT_FAILURE);
                 }
                 bytes_sent++;
           }

clock_gettime(CLOCK_REALTIME, &time2);
        temp = (time2.tv_nsec - time1.tv_nsec) ;
        long seconds = time2.tv_sec - time1.tv_sec;
        long nanoseconds = time2.tv_nsec - time1.tv_nsec;
        printf("Elapsed time: %ld seconds, %ld nanoseconds\n", seconds, nanoseconds);

        printf("time = %f ns\n", temp); 	
           close(pipe_fd);
            printf("Process %d, threadID %ld, bytes send %d\n", getpid(), pthread_self(), bytes_sent);
     }
     else
     {
           exit(EXIT_FAILURE);
     }
       sleep(1); // wait read thread to finish work
     printf("Process %d finished\n", getpid());
    return 0;
}
#endif


#if 0
int main()
{
     if(access(FIFO_NAME, F_OK) == -1)
     {
           int res = mkfifo(FIFO_NAME, 0777); //创建一个特殊的文件,FIFO
           if(res != 0)
           {
                 fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
                 exit(EXIT_FAILURE);
           }
     }

     int bytes_sent = 0;
     char buffer[BUFFER_SIZE + 1];
     printf("Process %d opening FIFO O_WRONLY\n", getpid());
       createReadThread();

   //打开FIFO 准备写
     int pipe_fd = open(FIFO_NAME, O_WRONLY);   // will block here, wait someone open the FIFO for read!
     printf("Process %d opened write fd %d\n", getpid(), pipe_fd);
     if(pipe_fd != -1)
     {
           while(bytes_sent < TOTAL_LEN)
           { //FIFO 就是按普通文件描述符来操作的.
                 int res = write(pipe_fd, buffer, BUFFER_SIZE);
                 if(res == -1)
                 {
                     fprintf(stderr, "Write error on pipe\n");
                     exit(EXIT_FAILURE);
                 }
                 bytes_sent += res;
           }
           close(pipe_fd);
            printf("Process %d, threadID %ld, bytes send %d\n", getpid(), pthread_self(), bytes_sent);
     }
     else
     {
           exit(EXIT_FAILURE);
     }
       sleep(1); // wait read thread to finish work
     printf("Process %d finished\n", getpid());
    return 0;
}
#endif




